﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour
{
    public static ObjectPool Instance => instance;
    private static ObjectPool instance;
    private const int initSpawnCount = 5;
    private List<GameObject> normalPlatformList = new List<GameObject>();
    private List<GameObject> commonPlatformList = new List<GameObject>();
    private List<GameObject> grassPlatformList = new List<GameObject>();
    private List<GameObject> winterPlatformList = new List<GameObject>();
    private List<GameObject> spikerPlatformLeftList = new List<GameObject>();
    private List<GameObject> spikerPlatformRightList = new List<GameObject>();
    private List<GameObject> deathEffectList = new List<GameObject>();
    private List<GameObject> diamondList = new List<GameObject>();

    private ManagerVars Vars;
    private void Awake()
    {
        instance = this;
        Vars = ManagerVars.Instance;
        Init();
    }

    private void Init()
    {
        GameObject go;
        for (int i = 0; i < initSpawnCount; i++)
        {
            go = Instantiate(Vars.normalPlatformPre, transform);
            go.SetActive(false);
            normalPlatformList.Add(go);

            for (int j = 0; j < Vars.commonPlatformGroup.Count; j++)
            {
                go = Instantiate(Vars.commonPlatformGroup[j], transform);
                go.SetActive(false);
                commonPlatformList.Add(go);
            }

            for (int j = 0; j < Vars.grassPlatformGroup.Count; j++)
            {
                go = Instantiate(Vars.grassPlatformGroup[j], transform);
                go.SetActive(false);
                grassPlatformList.Add(go);
            }

            for (int j = 0; j < Vars.winterPlatformGroup.Count; j++)
            {
                go = Instantiate(Vars.winterPlatformGroup[j], transform);
                go.SetActive(false);
                winterPlatformList.Add(go);
            }

            go = Instantiate(Vars.spikerPlatformLeft, transform);
            go.SetActive(false);
            spikerPlatformLeftList.Add(go);

            go = Instantiate(Vars.spikerPlatformRight, transform);
            go.SetActive(false);
            spikerPlatformRightList.Add(go);

            go = Instantiate(Vars.deathEffect, transform);
            go.SetActive(false);
            deathEffectList.Add(go);

            go = Instantiate(Vars.diamond, transform);
            go.SetActive(false);
            diamondList.Add(go);
        }
    }

    public GameObject GetNormalPlatform()
    {
        for (int i = 0; i < normalPlatformList.Count; i++)
        {
            if (!normalPlatformList[i].activeInHierarchy)
            {
                return normalPlatformList[i];
            }
        }

        GameObject go = Instantiate(Vars.normalPlatformPre, transform);
        go.SetActive(false);
        normalPlatformList.Add(go);
        return go;
    }

    public GameObject GetCommonPlatform()
    {
        for (int i = 0; i < commonPlatformList.Count; i++)
        {
            if (!commonPlatformList[i].activeInHierarchy)
            {
                return commonPlatformList[i];
            }
        }
        int ran = Random.Range(0, Vars.commonPlatformGroup.Count);
        GameObject go = Instantiate(Vars.commonPlatformGroup[ran], transform);
        go.SetActive(false);
        commonPlatformList.Add(go);
        return go;
    }


    public GameObject GetGrassPlatform()
    {
        for (int i = 0; i < grassPlatformList.Count; i++)
        {
            if (!grassPlatformList[i].activeInHierarchy)
            {
                return grassPlatformList[i];
            }
        }
        int ran = Random.Range(0, Vars.grassPlatformGroup.Count);
        GameObject go = Instantiate(Vars.grassPlatformGroup[ran], transform);
        go.SetActive(false);
        grassPlatformList.Add(go);
        return go;
    }

    public GameObject GetWinterPlatform()
    {
        for (int i = 0; i < winterPlatformList.Count; i++)
        {
            if (!winterPlatformList[i].activeInHierarchy)
            {
                return winterPlatformList[i];
            }
        }
        int ran = Random.Range(0, Vars.winterPlatformGroup.Count);
        GameObject go = Instantiate(Vars.winterPlatformGroup[ran], transform);
        go.SetActive(false);
        winterPlatformList.Add(go);
        return go;
    }

    public GameObject GetSpikerPlatformLeft()
    {
        for (int i = 0; i < spikerPlatformLeftList.Count; i++)
        {
            if (!spikerPlatformLeftList[i].activeInHierarchy)
            {
                return spikerPlatformLeftList[i];
            }
        }

        GameObject go = Instantiate(Vars.spikerPlatformLeft, transform);
        go.SetActive(false);
        spikerPlatformLeftList.Add(go);
        return go;
    }


    public GameObject GetSpikerPlatformRight()
    {
        for (int i = 0; i < spikerPlatformRightList.Count; i++)
        {
            if (!spikerPlatformRightList[i].activeInHierarchy)
            {
                return spikerPlatformRightList[i];
            }
        }

        GameObject go = Instantiate(Vars.spikerPlatformRight, transform);
        go.SetActive(false);
        spikerPlatformRightList.Add(go);
        return go;
    }

    public GameObject GetDeathEffect()
    {
        for (int i = 0; i < deathEffectList.Count; i++)
        {
            if (!deathEffectList[i].activeInHierarchy)
            {
                return deathEffectList[i];
            }
        }

        GameObject go = Instantiate(Vars.deathEffect, transform);
        go.SetActive(false);
        deathEffectList.Add(go);
        return go;
    }


    public GameObject GetDiamond()
    {
        for (int i = 0; i < diamondList.Count; i++)
        {
            if (!diamondList[i].activeInHierarchy)
            {
                return diamondList[i];
            }
        }

        GameObject go = Instantiate(Vars.diamond, transform);
        go.SetActive(false);
        diamondList.Add(go);
        return go;
    }

}
